/*
 * (C) APUE
 * Global definition and usually used head files
 */
#ifndef	_APUE_H
#define	_APUE_H

/*-----head files-----*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <signal.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/*-----head files(end)-----*/

/*-----global variant-----*/
#define	FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define	MAXLINE	1024
/*-----global variant(end)-----*/


typedef	void Sigfunc(int);	/* for signal handlers */

//#if defiend(SIG_IGN) && !defiend(SIG_ERR)
//#define	SIG_ERR	((Sigfunc *)-1)
//#endif

/*-------our own functions------------*/
void pr_exit(int status);
void pr_mask(const char *str);

void set_fl(int, int);
void clr_fl(int, int);

int lock_reg(int, int, int, off_t, int, off_t);
#define	read_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define	readw_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define	write_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define	writew_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define	un_lock(fd, offset, whence, len) \
	lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))

pid_t lock_test(int, int, off_t, int, off_t);
#define	is_read_lockable(fd, offset, whence, len) \
	(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define	is_write_lockable(fd, offset, whence, len) \
	lock_reg((fd), F_WRLCK, (offset), (whence), (len))

int lockfile(int);

ssize_t readn(int filedes, void *buf, size_t nbytes);
ssize_t writen(int filedes, void *buf, size_t nbytes);
/*-------our own functions------------*/

/*------error process funcitons------*/
void err_quit(const char* fmt, ...);
void err_msg(const char* fmt, ...);
/*------error process funcitons(end)------*/

#endif	/* _APUE_H */
